#include "window.h"

 Window::Window()
 { 	
#ifdef Q_WS_WIN
	file = path = QCoreApplication::applicationDirPath();
#else 
	path = QDir::homePath ()+"/.qalarm";
	imgfile = "/usr/share/qalarm/";
#endif	
 	Utils utils;
 	init();
 	createActions();
	createTrayIcon();
	trayIcon = new QSystemTrayIcon(this);
	playFile = new QProcess(this);
	url = new QUrl();	
	updateTrayIcon();
	repeat = getRepeat();	
	alarmTime = utils.readFile(path+"/data/alarm");	
	alarmDate = utils.readFile(path+"/data/date");	
	alarmDay = utils.readFile(path+"/data/day");		
	music = Phonon::createPlayer(Phonon::MusicCategory,Phonon::MediaSource(utils.readFile(path+"/data/audio")));
	Window::keyboardGrabber();	
	Utils::changed = false;
 }
 
//bool Window::changed = false; 
 
void Window::init()
{		
	timer = startTimer(1000);
	QTimerEvent * e = new QTimerEvent(timer);
	QCoreApplication::postEvent(this,e);	
}

void Window::keyPressEvent(QKeyEvent *event)
{
	Window::keyPressEvent(event);		
	switch (event->key()) {
		case Qt::Key_0:			
			//pushButton_0->animateClick(100);
			playAudio();
			break;
		}
}
 
void Window::createActions()
{
	setalarm = new QAction(tr("Set ala&rm"), this);
    connect(setalarm, SIGNAL(triggered()), this, SLOT(setAlarm()));

    setaudio = new QAction(tr("Set Au&dio"), this);
    connect(setaudio, SIGNAL(triggered()), this, SLOT(setAudio()));
    
    playaudio = new QAction(tr("&Play Audio"), this);
    connect(playaudio, SIGNAL(triggered()), this, SLOT(playAudio()));
    
    stopaudio = new QAction(tr("S&top Audio"), this);
    connect(stopaudio, SIGNAL(triggered()), this, SLOT(stopAudio()));
    
    seturl = new QAction(tr("Set U&rl"), this);
    connect(seturl, SIGNAL(triggered()), this, SLOT(setUrl()));
    
    openurl = new QAction(tr("Op&en Url"), this);
    connect(openurl, SIGNAL(triggered()), this, SLOT(openUrl()));
    
    aboutAction = new QAction(tr("&About"), this);
    connect( aboutAction, SIGNAL(triggered()), this, SLOT(_about()));
    
    aboutQtAction = new QAction(tr("About Qt"), this);
    connect( aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));

    quitAction = new QAction(tr("&Quit"), this);
    connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));   
}

void Window::createTrayIcon()
{	
	QColor *c = new QColor ( 238, 238, 157, 255 );	
	QPalette * p = new QPalette();
	p->setColor(QPalette::ToolTipBase,*c);
	QFont *f = new QFont ( "sans", 10, -1, false );	
	QToolTip::setFont ( *f );
	QToolTip::setPalette(*p);	
	trayIconMenu = new QMenu(this);	    
    trayIconMenu->addAction(setalarm);
    trayIconMenu->addAction(setaudio);
    trayIconMenu->addAction(playaudio);
    trayIconMenu->addAction(stopaudio);
    trayIconMenu->addAction(seturl);
    trayIconMenu->addAction(openurl);    
    trayIconMenu->addSeparator();
    trayIconMenu->addAction(aboutAction);
    trayIconMenu->addAction(aboutQtAction);
    trayIconMenu->addSeparator();
    trayIconMenu->addAction(quitAction); 
    stopaudio->setEnabled(false);  
    playaudio->setEnabled(true);		  
}

void Window::setAlarm()
{
	//Alarm * alarmWindow = new Alarm();
	alarmWindow.show();
}

void Window::setAudio()
{
	//Audio * audioWindow = new Audio();
    audioWindow.show();
}

void Window::playAudio()
{    
	Utils utils;
	music->setCurrentSource(utils.readFile(path+"/data/audio"));
	stopaudio->setEnabled(true);   
	playaudio->setEnabled(false);		
	music->play();		
}

void Window::stopAudio()
{  
	music->stop();
	stopaudio->setEnabled(false);   
	playaudio->setEnabled(true);	
}

void Window::setUrl()
{
   //Url * urlWindow = new Url();
   urlWindow.show();
}

void Window::openUrl()
{   		
	Utils utils;
	url->setUrl(utils.readFile(path+"/data/url"));
	QDesktopServices::openUrl(*url);
}

void Window::startAlarm()
{
	openUrl();
	playAudio();
}

void Window::timerEvent(QTimerEvent *e)
{
	Utils utils;	
	if (!e) return;
  	if (e->timerId() == timer){
  		if (Utils::changed) {
  			updateTrayIcon();
  			repeat = getRepeat();  			
  			alarmTime = utils.readFile(path+"/data/alarm");
  			alarmDate = utils.readFile(path+"/data/date");
  			alarmDay = utils.readFile(path+"/data/day");
  			Utils::changed = false;
		}		
  		QTime time = QTime::currentTime();
  		QDate date = QDate::currentDate();
    	QString strTime = time.toString("HH:mm:ss");
    	QString today = date.toString("dd/MMMM/yyyy");
    	int day = date.dayOfWeek();		
		if (alarmTime == strTime){		
			if (repeat == 0){
				if (today == alarmDate){
					startAlarm();
					repeat = -1;
				}				
			}
			else if (repeat == 1){
				if (day == alarmDay.toInt() + 1) startAlarm();
			}
			else if (repeat == 2){
				if (day < 6) startAlarm();
			}
			else if (repeat == 3){
				startAlarm();
			}
		}		
		if (music->state() == Phonon::PausedState) stopAudio();			
 	}
}

void Window::updateTrayIcon()
{
	Utils utils;
	repeat = getRepeat();	
	QString tooltipmsg1 = utils.readFile(path+"/data/audio");	
	QStringList list = tooltipmsg1.split("/");
	tooltipmsg1 = list[list.size()-1];
	QString tooltipmsg2 = utils.readFile(path+"/data/alarm");	
	QString tooltipmsg3 = utils.readFile(path+"/data/url");
	QString tooltipmsg4;		
	if (repeat == 1){		
		QString list[7] = {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
       	tooltipmsg4 = "Repeat once a week \n Day = "+list[utils.readFile(path+"/data/day").toInt()];	    
	}
	else if (repeat == 2){
		
      	tooltipmsg4 = "Repeat week days";
	}	
      	if (repeat == 3){		
       	tooltipmsg4 = "Repeat every days";	    
	}
	else if (repeat == 0 || repeat == -1){	         
        tooltipmsg4 = "Repeat once \n Date = "+utils.readFile(path+"/data/date");
	}
	QString message = " Alarm = "+tooltipmsg2+"\n Repeat = "+tooltipmsg4+"\n Audio = "+tooltipmsg1+"\n Url = "+tooltipmsg3;
	trayIcon->setIcon(QIcon(imgfile+"/images/alarm.png"));    
    trayIcon->setToolTip(message);
	trayIcon->show();	
	trayIcon->setContextMenu(trayIconMenu);    
}

void Window::_about()
{;
	QString cp = "©";
    QMessageBox::about(this, tr("About Alarm"),
             "<b> Alarm application V0.3.1</b> Copyright " +  QString::fromUtf8(cp.toLatin1().data()) + " 2008 Skander Jabouzi skander@skanderjabouzi.com<br>"
             	" This is a free software distributed under the terms of the GNU General Public License version 3\n(http://www.gnu.org/licenses/gpl-3.0.html)");     
}

int Window::getRepeat()
{
	Utils utils;
	int radio = utils.readFile(path+"/data/repeat").toInt();	
	return radio;
}
//
 
